package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesOrderMaterial;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesOrderMaterialDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:27:14
 */
public class MesOrderMaterialConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesOrderMaterialConverts.class);

    public static List<MesOrderMaterialDTO> convert(List<MesOrderMaterial> mesOrderMaterialList) {
        if (CollectionUtils.isEmpty(mesOrderMaterialList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesOrderMaterialDTO> mesOrderMaterialListResult = Lists.newArrayList();
        for (MesOrderMaterial mesOrderMaterial : mesOrderMaterialList) {
            mesOrderMaterialListResult.add(convert(mesOrderMaterial));
        }
        return mesOrderMaterialListResult;
    }

    public static MesOrderMaterialDTO convert(MesOrderMaterial mesOrderMaterial) {
        try {
            return BeanUtils.copy(mesOrderMaterial, MesOrderMaterialDTO.class);
        } catch (Exception e) {
            LOG.error("MesOrderMaterial到MesOrderMaterialDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesOrderMaterial到MesOrderMaterialDTO转换失败", e);
        }
    }

    public static MesOrderMaterial convert(MesOrderMaterialDTO mesOrderMaterialDTO) {
        try {
            return BeanUtils.copy(mesOrderMaterialDTO, MesOrderMaterial.class);
        } catch (Exception e) {
            LOG.error("MesOrderMaterialDTO到MesOrderMaterial转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesOrderMaterialDTO到MesOrderMaterial转换失败", e);
        }
    }
}